﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 单据规则
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-04-20
    /// </summary>
    public class BillRuleBll
    {
        private BillRuleIService service = new BillRuleService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<BillRuleEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<BillRuleEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<BillRuleEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 重复验证（名称）
        /// </summary>
        /// <param name="fullName">名称</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByFullName(string fullName, string id)
        {
            try
            {
                var whereLambda = ExtLinq.True<BillRuleEntity>();
                whereLambda = whereLambda.And(m => m.FullName == fullName);
                if (!string.IsNullOrEmpty(id))
                    whereLambda = whereLambda.And(m => m.Id != id);
                return await service.IsExist(whereLambda);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 重复验证（编码）
        /// </summary>
        /// <param name="enCode">编码</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByEnCode(string enCode, string id)
        {
            try
            {
                var whereLambda = ExtLinq.True<BillRuleEntity>();
                whereLambda = whereLambda.And(m => m.EnCode == enCode);
                if (!string.IsNullOrEmpty(id))
                    whereLambda = whereLambda.And(m => m.Id != id);
                return await service.IsExist(whereLambda);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(BillRuleEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(BillRuleEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(string id, BillRuleEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取单据流水号
        /// </summary>
        /// <param name="enCode">流水编码</param>
        /// <param name="isCache">是否缓存：每个用户会自动占用一个流水号，这个刷新页面也不会跳号</param>
        /// <returns></returns>
        public async Task<string> GetBillNumber(string enCode, bool isCache = false)
        {
            try
            {
                var strNumber = "";
                if (isCache == true)
                {
                    var cacheKey = CacheKey.BILLRULE + UserProvider.Instance.Get().UserId + enCode;
                    if (!CacheFactory.Cache.Exists(cacheKey))
                    {
                        strNumber = await service.GetNumber(enCode);
                        CacheFactory.Cache.Insert(cacheKey, strNumber);
                    }
                    else
                    {
                        strNumber = CacheFactory.Cache.Get(cacheKey);
                    }
                }
                else
                {
                    strNumber = await service.GetNumber(enCode);
                }
                return strNumber;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 使用单据流水号（注意：必须是缓存的单据才可以调用这个方法，否则无效）
        /// </summary>
        /// <param name="enCode">流水编码</param>
        public static void UseBillNumber(string enCode)
        {
            try
            {
                var cacheKey = CacheKey.BILLRULE + UserProvider.Instance.Get().UserId + enCode;
                CacheFactory.Cache.Remove(cacheKey);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}